package com.yingside.thread.copy;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class CopyFrame extends JFrame {
    /**进度条*/
    private JProgressBar bar;
    private JButton btnCopy;
    private JTextField txtFrom,txtTo;
    private JPanel p1,p2,p3;

    public CopyFrame() {
        this.setTitle("copy");
        this.setBounds(200,100,200,400);

        bar = new JProgressBar();
        bar.setMinimum(0);
        bar.setMaximum(100);
        //得到文件的大小以及当前拷贝的内容 做一个比例
        bar.setValue(0);

        p1 = new JPanel();
        p1.setLayout(new GridLayout(2,1,5,5));

        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        txtFrom = new JTextField(30);
        p2.add(txtFrom);
        p2.setBorder(new TitledBorder("From"));


        p3 = new JPanel();
        p3.setLayout(new BorderLayout());
        txtTo = new JTextField(30);
        p3.add(txtTo);
        p3.setBorder(new TitledBorder("To"));


        p1.add(p2);
        p1.add(p3);

        btnCopy = new JButton("拷贝");

        btnCopy.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CopyThread copyThread = new CopyThread();
                Thread t = new Thread(copyThread);
                t.start();
                btnCopy.setEnabled(false);
            }
        });

        this.add(p1,BorderLayout.CENTER);
        this.add(bar,BorderLayout.NORTH);
        this.add(btnCopy,BorderLayout.SOUTH);

        pack();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public class CopyThread implements Runnable{

        @Override
        public void run() {
            int value= 0;
            String fromTxt = txtFrom.getText();
            String toTxt = txtTo.getText();

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;

            try {
                bis = new BufferedInputStream(
                        new FileInputStream(
                                new File(fromTxt)
                        )
                );

                bos = new BufferedOutputStream(
                        new FileOutputStream(
                                new File(toTxt)
                        )
                );

                byte [] bs = new byte[1024];
                int b = 0;
                long vLength = 0;
                //获取输入流读取的总字节数
                //注意返回的是一个int类型，但是我们不能用int类型接收
                //因为如果文件过大，int类型保存不了
                long total = bis.available();

                while((b = bis.read(bs)) != -1){
                    vLength += b; //每一次读取的字节数量进行累加
                    value = (int)(vLength * 100 / total) ;
                    bar.setValue(value);
                    //往外写文件
                    bos.write(bs);
                    if(value == 100){
                        value = 0;
                        JOptionPane.showMessageDialog(null,"拷贝完成");
                        bar.setValue(value);
                        btnCopy.setEnabled(true);
                        break;
                    }
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(bis != null){
                        bis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    if(bos != null){
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }


    public static void main(String[] args) {
        CopyFrame bf = new CopyFrame();
    }

}
